import request from '@/util/axios';
import type { File_type, file_mergeType } from './file_type';
// 统一管理接口
enum API {
  FileUpload = '/api/asset_registration/file/upload', //资产登记文件上传
  FileMerge = '/api/asset_registration/file/merge' //资产登记文件合并
}
// 资产登记文件上传
export async function file_upload(data: File_type) {
  try {
    return await request.post(API.FileUpload, { ...data });
  } catch (error) {
    console.error('登录时发生错误：', error);
    throw new Error('登录失败，请稍后重试。');
  }
}
// 资产登记文件合并
export async function file_merge(data: file_mergeType) {
  try {
    return await request.post(API.FileMerge, { ...data });
  } catch (error) {
    console.error('登录时发生错误：', error);
    throw new Error('登录失败，请稍后重试。');
  }
}
//   ------------------------最新文件上传-----------------------------------
// 1.文件预准备
export function getStatrFunc(path: string) {
  return async function DataSourceUpload(data: File_type) {
    try {
      return await request.post(path, { ...data });
    } catch (error) {
      console.error('上传初始化请求失败', error);
      throw new Error('上传初始化请求失败');
    }
  };
}
//  2.文件分片
export function getUploadFunc(path: string) {
  return async function DataSourceUpload(data: FormData) {
    try {
      // return await request.post(path, { ...data });
      return await request.postForm(path, data, {
        headers: { 'Content-Type': 'multipart/form-data' }
      });
    } catch (error) {
      console.error('上传初始化请求失败', error);
      throw new Error('上传初始化请求失败');
    }
  };
}
//  3.文件合并
export function getMergeFunc(path: string) {
  return async function DataSourceMerge(data: file_mergeType) {
    try {
      return await request.post(path, { ...data });
    } catch (error) {
      console.error('合并请求失败', error);
      throw new Error('合并请求失败');
    }
  };
}
